package com.example.backend.controller;



import com.example.backend.entity.Car;
import com.example.backend.entity.ResponseMessage;
import com.example.backend.service.CarService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


@RestController
public class CarController {

    @Autowired
    CarService carService;


    @PostMapping("/car")
    public int add(@RequestBody Car car) {
        System.out.println("添加购物车");
        return carService.add(car);
    }

    @DeleteMapping("/car/{car_num}")
    public ResponseMessage delete(@PathVariable String car_num){
        if(carService.delete(car_num)){
            return ResponseMessage.success(null);
        }
        else{
            return ResponseMessage.error();
        }
    }

    @PutMapping("/car")
    public ResponseMessage update(@RequestBody Car car){
        if(carService.update(car)){
            return ResponseMessage.success(car);
        }
        else{
            return ResponseMessage.error();
        }
    }



    @GetMapping("/car/{car_num}")
    public ResponseMessage queryAll(@PathVariable String car_num) {
        return ResponseMessage.success(carService.queryAll(car_num));
    }

    /** 加购 查询购物车中是否存在此商品 加购 */
    @GetMapping("/car/exist")
    public int queryExist(Car car) {
        System.out.println("查询购物车中商品是否存在");
        if(carService.queryExist(car)>0)
            return carService.updateAdd(car);
        else
            return carService.add(car);
    }
    /** 加购 */
    @PutMapping("/car/add")
    public int updateAdd(@RequestBody Car car) {
        System.out.println("加购");
        if(carService.queryExist(car)>0)
            return carService.updateAdd(car);
        else
            return carService.add(car);
    }
    /** 减购 */
    @GetMapping("/car/jian")
    public int updateJian(Car car) {
        System.out.println("减购");
        return carService.updateJian(car);
    }
    /** 查询购物车 */
    @GetMapping("/car/selectCars")
    public ArrayList<Car> selectCars(Car car) {
        System.out.println("查询购物车");
        return carService.selectCars(car);
    }
    /** 修改购物车中的商品数量 */
    @PutMapping("/car/updateShopNum")
    public int updateShopNum(@RequestBody Car car) {
        System.out.println("修改购物车中的商品数量");
        return carService.updateShopNum(car);
    }
}

